package com.devicemantra.coordinate.util;

public class CXMLBuilder {
	
		private String mXml = null;
		private boolean mDebugOn = false;
		
		private static final String START_ANGLE_BRACKET = 	"<";
		private static final String BACK_SLASH	=		"/";
		private static final String CLOSE_ANGLE_BRACKET = 	">";

		public CXMLBuilder()
		{
			mXml = new String();
			AddVersionEncoding();
			
		}
		
		public void AddCRLF()
		{
			mXml = mXml.concat(" \n");
		}
		
		public void AddVersionEncoding()
		{
			mXml = mXml.concat("<?xml version='1.0' encoding='utf-8' ?>");
			AddCRLF();
			System.out.println("Added version and encoding string -> " + mXml.length() + " " + mXml);
			
		}

		public void AddOpeningTag(String pOpeningTag)
		{
			mXml = mXml.concat(CXMLBuilder.START_ANGLE_BRACKET);
			mXml = mXml.concat(pOpeningTag);
			mXml = mXml.concat(CXMLBuilder.CLOSE_ANGLE_BRACKET);
			
			if( mDebugOn )
				log(mXml);

		}

		public void AddData(String pData)
		{
			if(pData == null)
			{
				pData = "NA";
			}
			mXml = mXml.concat(pData);
			
			if( mDebugOn )
				log(mXml);
		}

		public void AddClosingTag(String pClosingTag)
		{
			mXml = mXml.concat(CXMLBuilder.START_ANGLE_BRACKET);
			mXml = mXml.concat(CXMLBuilder.BACK_SLASH);
			mXml = mXml.concat(pClosingTag);
			mXml = mXml.concat(CXMLBuilder.CLOSE_ANGLE_BRACKET);
			if( mDebugOn )
				log(mXml);
		}

		public void AddNode(String pElementName, String pElementData)
		{
			AddOpeningTag(pElementName);
			AddData(pElementData);
			AddClosingTag(pElementName);
			AddCRLF();
			
			if( mDebugOn )
				log(mXml);
		}

		public String getXml()
		{
			return mXml;
		}

		public void reset()
		{
			if( mXml == null)
				mXml = new String();
			else
				mXml = "";
		}

		public void setDebug(boolean pIsOn)
		{
			mDebugOn = pIsOn;
		}

		public String toString()
		{
			return mXml;
		}

		public void log(String pLogMsg)
		{
			System.out.println(pLogMsg);
		}
};




